<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == currIndex }"
            v-for="(item, index) in options1"
            :key="index"
            @mouseenter="updateMinAndMaxFn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: index == currIndex2 }"
            v-for="(item, index) in options2"
            :key="index"
            @mouseenter="updateMinAndMaxFn2(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="productListShow" />
      <i class="iconfont icon-loading" v-show="loading">加载中.....</i>
      <div v-show="!loading">加载完成</div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GetProductsAPI } from "@/request/api";
import { getScrollHeight, getClientHeight, getScrollTop } from "@/utils";
export default {
  data() {
    return {
      productList: {},
      productListShow: {},
      serchProduct: {
        did: 0,
        type: 0,
        min: 0,
        max: 100000,
        keyword: "",
      },
      loading: true,
      page: 1,
      size: 8,
      currIndex: 0,
      currIndex2: 0,
      options2: [
        { txt: "全部", type: 0 },
        { txt: "实物礼品", type: 1 },
        { txt: "虚拟礼品", type: 2 },
      ],
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      isReachBottom: true,
    };
  },
  created() {
    this.isReachBottom = true;
    this.loading = true;
    if (this.productList.length == this.productListShow.length) {
      this.loading = false;
    }
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    scrollFn() {
      //getScrollTop滚动距离 getClientHeight用户可视区域 getScrollHeight整个文档高度
      if (this.productList.length == this.productListShow.length) {
        this.loading = false;
      }
      if (
        getScrollHeight() - 240 < getScrollTop() + getClientHeight() &&
        this.isReachBottom == true
      ) {
        this.isReachBottom = false;
        this.page++;
        setTimeout(() => {
          for (
            let i = (this.page - 1) * this.size;
            i < this.page * this.size;
            i++
          ) {
            console.log("i的值", i);
            this.productList[i] &&
              this.productListShow.push(this.productList[i]);
            this.isReachBottom = true;
          }
        }, 2000);
      }
    },
    updateMinAndMaxFn(index, min, max) {
      this.currIndex = index;
      this.serchProduct.min = min;
      this.serchProduct.max = max;
      this.GoodsSearch();
    },
    updateMinAndMaxFn2(index, type) {
      this.currIndex2 = index;
      this.serchProduct.type = type;
      this.GoodsSearch();
    },
    async GoodsSearch() {
      let res = await GetProductsAPI(this.serchProduct);
      if (!res) return;
      this.productList = res.data.data;
      //获取前8条数据
      this.productListShow = this.productList.filter(
        (item, index) => index < this.size //* this.page
      );
    },
  },
  components: {
    Crumb,
    List,
  },
  async created() {
    //进入页面请求商品API获取数据修改data中的值
    this.GoodsSearch();
    this.$eventBus.$on("searchProduct", async (keyword) => {
      this.$router.push("/goods");
      this.serchProduct.keyword = keyword;
      this.GoodsSearch();
    });
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>